package com.yunjie.service.comprofile;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yunjie.dao.profile.ProfileDao;
import com.yunjie.model.ComProfile;
import com.yunjie.model.tree.NodeModel;
import com.yunjie.service.TreeHelper;

@Service("profileService")
@Transactional
public class ProfileServiceImpl extends TreeHelper<ComProfile> implements
		ProfileService {
	@Autowired
	private ProfileDao profileDao;

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public NodeModel getTreeModel(Integer id) throws Exception {
		// TODO Auto-generated method stub
		ComProfile profile = this.findById(id);
		return this.getCurrentNodeWithChildrens(profile);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Set<NodeModel> getSubNodes(Integer id) throws Exception {
		// TODO Auto-generated method stub
		ComProfile profile = this.findById(id);
		return this.getOneChildNodes(profile);
	}
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Set<NodeModel> getNodesByLevel(Integer level) throws Exception {
		List<ComProfile> profiles = profileDao.findByLevel(level);
		Set<ComProfile> sets = new HashSet<ComProfile>(0);
		for(ComProfile pro : profiles){
			sets.add(pro);
		}
		return this.getCurrentNodes(sets);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public ComProfile save(ComProfile profile, Integer parentId) throws Exception {
		// TODO Auto-generated method stub
		List<ComProfile> list = profileDao.findByNameOrCode(profile.getName(),
				profile.getCode());
		if (list.isEmpty()) {
			ComProfile parentProfile = findById(parentId);
			profile.setComProfile(parentProfile);
			profile = profileDao.save(profile);
			return profile;
		} else {
			return null;
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public ComProfile updata(ComProfile profile, Integer parentId) throws Exception {
		// TODO Auto-generated method stub
		ComProfile c = profileDao.findOne(profile.getId());
		c.setCode(profile.getCode());
		c.setFname(profile.getFname());
		c.setLevel(profile.getLevel());
		c.setName(profile.getName());
		c.setNote(profile.getNote());
		c.setOpid(profile.getOpid());
		c.setOrgcode(profile.getOrgcode());
		c.setSname(profile.getSname());
		c.setStatus(profile.getStatus());
		c.setTs(profile.getTs());
		
		c.setComProfile(findById(parentId));
		
		profile = profileDao.save(c);
		
		return profile;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public boolean delete(Integer id) throws Exception {
		// TODO Auto-generated method stub
		profileDao.delete(id);
		return true;
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public ComProfile findById(Integer id) throws Exception {
		// TODO Auto-generated method stub
		return profileDao.findOne(id);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<ComProfile> findByParentId(Integer parentId) throws Exception {
		// TODO Auto-generated method stub
		return profileDao.findByComProfileId(parentId);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<ComProfile> findByParentId(Integer parentId, Integer page,
			Integer start, Integer limit) throws Exception {
		// TODO Auto-generated method stub
		//Pageable pageable = new PageRequest(page,limit);
		return profileDao.findByComProfileId(parentId);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<ComProfile> findAll(Integer page, Integer start, Integer limit)
			throws Exception {
		// TODO Auto-generated method stub
		//Pageable pageable = new PageRequest(page,limit);
		return (List<ComProfile>) profileDao.findAll();
	}

}
